export default {
  namespaced: true,
  state() {
    return {
      type:'all',
      list: JSON.parse(localStorage.getItem("todos")) || [],
    };
  },
  mutations: {
    changeDone(state, id) {
      const obj = state.list.find((item) => item.id === id);
      obj.isDone = !obj.isDone;
    },
    delById(state, id) {
      state.list = state.list.filter((item) => item.id !== id);
    },
    add(state, content) {
      state.list.push({
        id: +new Date(),
        name: content,
        isDone: false,
      });
    },
    changeIsAll(state,val){
      state.list.forEach(item=>item.isDone=val)
    },
    changeType(state,val){
      state.type=val
    },
    clearDone(state){
      state.list=state.list.filter(item=>!item.isDone)
    }
  },
  actions: {
    clearDoneAsync(content){
        setTimeout(() => {
          content.commit('clearDone')
        }, 1000);
    }
  },
  getters: {
    leftCount(state){
      return state.list.filter(item=>!item.isDone).length
    },
    showClear(state){
      return state.list.some(item=>item.isDone)
    },
    showList(state){
      if(state.type==="active"){
        return state.list.filter(item=>!item.isDone)
      }else if(state.type==="complele"){
        return state.list.filter(item=>item.isDone)
      }else {
        return state.list
      }
    }
  }
};
